package com.daylightrobbery.server.handler;

import com.daylightrobbery.server.packet.GamePacket;
import com.daylightrobbery.utils.ObjectMapperUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class PacketManager {
    private static final Logger log = LoggerFactory.getLogger(PacketManager.class);

    private static ConcurrentHashMap<String, PlayerPacketManager> playerPacketManagerMap = new ConcurrentHashMap<>();

    public static long generateId(String uid){
        PlayerPacketManager manager = playerPacketManagerMap.get(uid);
        if (manager == null) {
            throw new RuntimeException();
        }
        long id = manager.generateId();
        return id;

    }

    public static void register(String uid, long start){
        PlayerPacketManager manager = new PlayerPacketManager(start);
        playerPacketManagerMap.put(uid, manager);
    }

    public static List<GamePacket> processGamePackets(GamePacket packet){
        String uid = packet.getUid();
        PlayerPacketManager manager = playerPacketManagerMap.get(uid);
        if (manager == null) {
            log.info("error: " + ObjectMapperUtil.toJSON(packet));
            throw new RuntimeException();
        }
        return manager.processGamePacket(packet);
    }

}
